<!--头部公共部分-->
{include file="common/header" /}

<div id="dm_arrang" class="x-body" class="clear:both;">
    <div class="layui-form layui-col-md12 x-so layui-form-pane" >
        <button class="layui-btn layui-btn-primary" id="return_course_lists">
            <i class="layui-icon layui-icon-prev"></i>返回班级列表</button>
        <div class="layui-input-inline" style="float: right;margin-right: 20px;">
            <script>
                var status  = '{$grades_result.status}';
                if(status == 1){
                    document.write('<button class="layui-btn layui-btn-disabled"><i class="layui-icon layui-icon-file-b"></i><span title="已开班的班级无法修改排课信息">修改排课<sup><span class="layui-badge-dot" style="position: relative; top: -3px; left: -3px;"></span></sup></span></button>');
                }else{
                    document.write('<button class="layui-btn layui-btn-primary" id="edit_grades"><i class="layui-icon layui-icon-file-b"></i>修改排课</button>');
                }
            </script>
          <!--
            <button class="layui-btn layui-btn-primary" id="edit_grades"><i class="layui-icon layui-icon-file-b"></i>编辑班级</button>
            -->
            <button class="layui-btn layui-btn-danger" id="delete_grades">结业</button>
        </div>
    </div>


    <fieldset class="layui-elem-field" style="border-width: 2px; height: 210px;  line-height: 50px; ">
        <span style="color: #00b7ee;font-size: 18px;  margin-left: 10px; ">{$grades_result.grades_name}</span>
        <hr style="margin-right: 10px; margin-left: 10px;">
        <div class="layui-row">
            <div class="layui-col-md3"  style="margin-left: 10px;">
                <div style="line-height:22px;">
                    <span style="font-size: 14px;">课程类别：{$grades_result.course_name}</span><br/>
                    <span style="font-size: 14px;">班主任：{$grades_result.headmaster}</span><br/>
                    <span style="font-size: 14px;">任课老师：{$grades_result.teacher}</span><br/>
                    <span style="font-size: 14px;">助教：{$grades_result.assistant}</span><br/>
                    <span style="font-size: 14px;">招生情况：已招{$grades_result.enrollment_status}人</span><br/>
                    <span style="font-size: 14px;color: #f7333d">状态：{$grades_result.grades_status}</span>
                </div>
            </div>
            <div class="layui-col-md3" style="margin-left: 10px;">
                <div style="line-height:22px">
                    <span style="font-size: 14px;">开班时间:{$grades_result.class_start_date}</span><br/>
                    <span style="font-size: 14px;">结业时间:{$grades_result.class_end_date}</span><br/>
                    <span style="font-size: 14px;">计划排课：{$grades_result.project_num}次</span><br/>
                    <span style="font-size: 14px;">已经排课：{$grades_result.already_class}次</span><br/>
                    <span style="font-size: 14px;">上课进度：{$grades_result.already_arranged}次</span><br/>
                    <span style="font-size: 14px;">上课教室：{$grades_result.room_text}</span>

                </div>
            </div>
            <div class="layui-col-md3" style="margin-left: 10px;">
                <div style="line-height:22px">
                    <span style="font-size: 14px;">排座状态：{$grades_result.seat_num}</span><br/>
                    <span style="font-size: 14px;">归属校区：{$grades_result.campus_name}</span><br/>
                    <span style="font-size: 14px;">创建时间：{$grades_result.create_time}</span><br/>
                    <span style="font-size: 14px;">备注：{$grades_result.remarks}</span>
                </div>
            </div>
        </div>
    </fieldset>

<div class="layui-tab layui-tab-card" lay-filter="filter">
        <ul class="layui-tab-title">
            <li class="layui-this">课表</li>
            <li>学员列表</li>
        </ul>
        <div class="layui-tab-content" >
            <div class="layui-tab-item layui-show">
                    <table class="layui-table" id="datalist" lay-filter="Event"></table>
                    <script type="text/html" id="name">
                    <a lay-event="see_info" class="layui-table-link">{{ d.class_grades_name }}</a>
                    </script>
                    <script type="text/html" id="barTable">
                            {{# if(d.status == 1){ }}
                            <a class="layui-btn layui-btn-xs " lay-event="cancel_dm">撤销上课</a>
                            {{# } else { }}
                            <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="dm">点名上课</a>
                            {{# } }}

                            {{# if(d.is_arrange == 1){ }}
                            <a class="layui-btn layui-btn-xs" lay-event="look_work">查看作业</a>
                            {{# } else { }}
                            <a class="layui-btn layui-btn-xs layui-btn-disabled">暂无作业</a>
                            {{# } }}

                            <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="look_comment">查看点评</a>

                            {{# if(d.status == 1){ }}
                            <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="modify_student">修改上课信息</a>
                            {{# } else { }}
                            <a class="layui-btn layui-btn-xs layui-btn-disabled"><span title="该课节还未【点名上课】无上课信息">修改上课信息<sup><span class="layui-badge-dot" style="position: relative; top: -3px; left: -3px;"></span></sup></span></a>
                            {{# } }}


                            {{# if(d.status == 1){ }}
                            <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="change_content">修改上课内容</a>
                            {{# } else { }}
                            <a class="layui-btn layui-btn-xs layui-btn-disabled"><span title="该课节还未【点名上课】无上课内容">修改上课内容<sup><span class="layui-badge-dot" style="position: relative; top: -3px; left: -3px;"></span></sup></span></a>
                            {{# } }}



                            {{# if(d.status == 1){ }}
                            <a class="layui-btn layui-btn-xs layui-btn-disabled" ><span title="已上课班级无法操作【删除】功能">删除<sup><span class="layui-badge-dot" style="position: relative; top: -3px; left: -3px;"></span></sup></span></a>
                            {{# } else { }}
                            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
                            {{# } }}
                        </script>
            </div>
            <div class="layui-tab-item ">
                <table class="layui-table" id="datalist_students" lay-filter="Event"></table>
            </div>
        </div>

</div>
</div>
</div>
<!--底部公共部分-->
{include file="common/footer" /}
<script>
    layui.use(['table', 'form','element',  'laydate', 'layer'], function () {
        var table = layui.table,
            laydate = layui.laydate,
            form = layui.form,
            element = layui.element,
            layer = layui.layer,
            $ = layui.$;

        var loading = layer.load();
        form.render(null, 'form-organization');
        form.on('submit(form-organization-search)', function (data) {
            var searchLoading = layer.load();
            table.reload('table-organization', {
                url: "{:url('Arranging/lists')}"
                , where: data.field
                , done: function () {
                    layer.close(searchLoading);
                }
            });
            return false;
        });

        $('#table-add').click(function () {
            var url = "{:url('Arranging/add')}";
            location.href = url;
        });
        //返回课程
        $('#return_course_lists').click(function () {
            var grades_status = '{$grades_status}';
            var course_id = '{$grades_result.course_id}';
            var campus_id = '{$campus_id}';

            if(grades_status == 1){
                location.href = "{:url('grades/lists')}";
            }else{
                location.href = "{:url('grades/grades_course')}?course_id="+course_id+'&campus_id='+campus_id;
            }

        });
        //日期
        laydate.render({
            elem: '#start',
            range:true
            ,trigger: 'click'
        });

        var grades_id = '{$grades_id}';  //班级ID
        var campus_id = '{$campus_id}';  //班级ID
        //方法级渲染
        var tableIns = table.render({
            elem: '#datalist'
            , url: '{:url("Arranging/arrang_course")}?grades_id=' + grades_id+'&campus_id='+campus_id
            , method: 'POST'
            , loading: true
            , parseData: function (res) { //res 即为原始返回的数据
            if (res.code === 1) {
                res.code = 0;
            } else {
                res.code = -1;
            }
            return {
                "code": res.code, //解析接口状态
                "msg": res.message, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.data //解析数据列表
            };
            },
            cols: [[

                {field: 'id', title: 'ID', width: 100, align: 'center'
                }, {field: 'course_class_name', style:'color:#01AAED;font-weight: bold;',title: '课节名称', minWidth: 300, align: 'center'
                }, {field: 'course_date_week_time', title: '上课时间', width: 300, align: 'center'
                }, {field: 'teacher', title: '授课老师', width: 100, align: 'center'
                }, {field: 'class_room', title: '教室', width: 100, align: 'center'
                }, {field: 'class_length', title: '上课时长', width: 100, align: 'center'
                }, {field: 'actual_number', title: '实到人数', width: 100, align: 'center'
                },{fixed: 'right', title: '操作', width: 600, align: 'center', toolbar: '#barTable'}
            ]]
            , height: 'full-180'
            , page: true
            , done: function () {
                $("[data-field='id']").css('display','none');
                layer.close(loading);
             }
        });


        //方法级渲染
        var tableIns_students = table.render({
            elem: '#datalist_students'
            , url: '{:url("Arranging/gradesstudentslists")}?grades_id=' + grades_id+'&campus_id='+campus_id
            , method: 'POST'
            , loading: true
            , parseData: function (res) { //res 即为原始返回的数据
                if (res.code === 1) {
                    res.code = 0;
                } else {
                    res.code = -1;
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            },
            cols: [[

                {field: 'id', title: 'ID', width: 100, align: 'center'
                },{field: 'student_name', style:'color:#01AAED;font-weight: bold;',title: '学员姓名', width: 250, align: 'center'
                }, {field: 'student_type_text', title: '学员状态', width: 300, align: 'center'
                }, {field: 'pay_status_text', title: '付款状态', width: 300, align: 'center'
                }, {field: 'class_date_max', title: '最近到课日期', width: 150, align: 'center'
                }, {field: 'class_nums', title: '购买课总次/时', width: 150, align: 'center'
                }, {field: 'class_arrangement_num', title: '排课课总次/时', width: 150, align: 'center'
                }, {field: 'class_arrangement_surplus', title: '剩余总课次/时', width: 150, align: 'center'
                },
            ]]
            , height: 'full-180'
            , page: true
            , done: function () {
                $("[data-field='id']").css('display','none');
                layer.close(loading);
            }
        });

        //监听工具条
        table.on('tool(Event)', function (obj) {
            var data = obj.data; //获得当前行数据
            console.log(data)
            var layEvent = obj.event; //获得 lay-event 对应的值
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            if (layEvent === 'dm') { //查看
                var title = '点名上课';
                if(data.class_grades_name.indexOf("补课班级") != -1){
                    title = '点名上课【补课】';
                }

                /*
                let index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
                parent.location.href = "{:url('Arranging/roll_call')}?id="+data.id;
                */

                layer.open({
                    skin: 'layui-layer-lan',
                    type: 2,
                    closeBtn: 2,
                    title: title,
                    area: ['99%', '95%'],
                    shadeClose: false,
                    content: "{:url('Arranging/roll_call')}?id="+data.id+'&campus_id='+campus_id,
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);
                        body.find("input[name='grades_name']").val(data.class_grades_name);
                        body.find("input[name='class_time']").val(data.class_date);
                    },
                    cancel: function(index, layero){
                        var field = {};
                        field.id = data.id;
                        $.ajax({
                            url: '/admin/arranging/close',
                            method: "post",
                            data: field,
                            beforeSend: function () {
                                isLoad = layer.load(0, {
                                    shade: false
                                }); //0代表加载的风格，支持0-2
                            },
                            success: function (result) {
                                layer.msg(result.msg);
                                if (result.code === 1) {
                                    layer.close(index)
                                    tableIns.reload();
                                    tableIns_students.reload();

                                }

                            },
                            complete: function () {
                                layer.close(isLoad)
                            },
                            error: function (error) {
                                console.log(error)
                            }
                        });
                        return false;
                    },

                    end:function () {
                        tableIns_students.reload();
                        tableIns.reload();
                    }

                });


            }else if(layEvent === 'del'){
                layer.confirm('真的删除该节课么?', function (index) {
                    $.post("{:url('Arranging/class_del')}", {
                        id: data.id
                    }, function (result) {
                        if (result.code === 1) {
                            layer.msg(result.msg, {
                                time: 2000
                            });
                             tableIns.reload();
                            tableIns_students.reload();

                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 2000
                            });
                        }
                    }, "json")
                });

            }else if(layEvent === 'change_content'){  //修改上课内容

                layer.open({
                    skin: 'layui-layer-lan',
                    type: 2,
                    closeBtn: 2,
                    title: '修改上课内容',
                    area: ['50%', '65%'],
                    shadeClose: false,
                    content: "{:url('Arranging/get_content')}?id="+data.id+'&campus_id='+campus_id
                });

            }else if(layEvent === 'cancel_dm'){     //撤消排课
                layer.confirm('真的撤销该节课么?', function (index) {
                    $.post("{:url('Arranging/cancel_grades')}", {
                        id: data.id
                    }, function (result) {
                        if (result.code === 1) {
                                layer.msg(result.msg, {
                                    time: 2000
                                });
                            tableIns.reload();
                            tableIns_students.reload();

                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 2000
                            });
                        }
                    }, "json")
                });
            }else if(layEvent === 'see_info'){
                //修改上课信息

            }else if(layEvent === 'modify_student'){

                layer.open({
                    skin: 'layui-layer-lan',
                    type: 2,
                    closeBtn: 2,
                    title: '修改上课信息',
                    area: ['99%', '95%'],
                    shadeClose: false,
                    content: "{:url('Arranging/modify_student')}?id="+data.id+'&campus_id='+campus_id,
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);
                        body.find("input[name='grades_name']").val(data.class_grades_name);
                        body.find("input[name='class_time']").val(data.class_date);
                    },
                    cancel: function(index, layero){

                    },

                    end:function () {

                        tableIns.reload();
                        tableIns_students.reload();
                    }

                });

            }else if(layEvent === 'look_work'){
                layer.open({
                    skin: 'layui-layer-lan',
                    type: 2,
                    closeBtn: 2,
                    title: '查看作业',
                    area: ['90%', '90%'],
                    shadeClose: false,
                    content: "{:url('Arranging/look_work')}?id="+data.id,
                    end:function () {
                        tableIns.reload();
                    }
                });
            }else if(layEvent === 'look_comment'){
                layer.open({
                    skin: 'layui-layer-lan',
                    type: 2,
                    closeBtn: 2,
                    title: '查看点评',
                    area: ['90%', '90%'],
                    shadeClose: false,
                    content: "{:url('Arranging/look_comment')}?id="+data.id,
                    end:function () {
                        tableIns.reload();
                    }
                });
            }
        });

        //结业
        $('#delete_grades').click(function () {
            layer.confirm('结业将会删除相关班课记录,确定结业吗?', function (index) {
                $.post("{:url('Grades/del')}", {
                    id: grades_id
                }, function (result) {
                    if (result.code === 1) {
                        layer.msg(result.msg, {
                            time: 2000
                        });
                        setTimeout(function () {
                            var course_id = '{$grades_result.course_id}';
                            var grades_status = '{$grades_status}';
                            if(grades_status == 1){
                                location.href = "{:url('grades/lists')}";
                            }else{
                                location.href = "{:url('grades/grades_course')}?course_id="+course_id;
                            }

                        }, 1000)
                    } else {
                        layer.msg(result.msg, {
                            icon: 2,
                            time: 1000
                        });
                    }
                }, "json")
            });
        });

        //修改排课
        $('#edit_grades').click(function () {
            layer.open({
                type: 2,
                closeBtn: 2,
                title: '',
                area: ['90%', '70%'],
                shadeClose: false,
                content: '{:url("Grades/change_grades")}?id='+grades_id + '&status=' + 2+'&campus_id='+campus_id,

                end:function () {
                    tableIns.reload();
                    tableIns_students.reload();
                },


            });

            /*
            layer.confirm('修改排课信息将重新更新已有班级信息,确认修改吗?', function (index) {
                layer.open({
                    type: 2,
                    closeBtn: 2,
                    title: '',
                    area: ['90%', '75%'],
                    shadeClose: false,
                    content: '{:url("Grades/change_grades")}?id='+grades_id + '&status=' + true,
                    end:function () {
                        location.href = "{:url('course/lists')}";
                    }
                });
            });
            */

        });



        form.on('switch(sexDemo)', function(obj){
            console.log(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis)
            var status;
            if (obj.elem.checked == true) {
                status = 0;
            } else {
                status = 1;
            }
            //激活与禁用状态改变
            $.post("{:url('Arranging/arranging_check_status')}", {
                id: this.value,
                status: status
            }, function (rJson) {
                if (rJson.code === 0) {
                    layer.msg(rJson.msg, {
                        time: 2000
                    });
                    setTimeout(function () {
                        location.reload();
                    }, 2000)
                } else {
                    layer.msg(rJson.msg, {
                        icon: 2,
                        time: 2000
                    });
                }
            }, "json")
        });

    });
</script>

